const _request = require("../../utils/request.js"); 
import {areaList} from './areaconfig';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    name: '',
    phone: '',
    province: '广东省',
    city: '深圳市',
    region: '福田区',
    detailAddress: '',
    areaList: areaList,
    areaCode: '440304',
    selectVisible: false,
    receiveStatus: 1,
    addFlag: true,
    id: '',
    mallimgUrl: app.globalData.mallimgUrl
  },

  switchDefaultFlag() {
    if (this.data.receiveStatus === 1) {
      this.setData({
        receiveStatus: 0
      })
    } else {
      this.setData({
        receiveStatus: 1
      })
    }
  },

  showSelectArea() {
    this.setData({
      selectVisible: true
    })
  },

  hideSelectArea() {
    this.setData({
      selectVisible: false
    })
  },

  save() {
    if (!this.data.name) {
      wx.showToast({
        title: '请输入收件人名',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!this.data.phone) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!this.data.province || !this.data.city || !this.data.region) {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!this.data.detailAddress) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.addFlag) {
      this.saveAddress()
    } else {
      this.updateAddress()
    }
    
  },

  updateAddress() {
    const self = this
    _request.postObj('/receiveAndDeliver/updateReceiveAndDeliverAddressById', {
      id: this.data.id,
      receiveStatus: this.data.receiveStatus,
      name: this.data.name,
      phone: this.data.phone,
      province: this.data.province,
      city: this.data.city,
      region: this.data.region,
      detailAddress: this.data.detailAddress,
      regionalCode: this.data.areaCode
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '修改收货地址成功',
          icon: 'none',
          duration: 1000
        })
        wx.navigateBack({
          delta: 0,
        })
      }
    })
  },

  saveAddress() {
    const self = this
    _request.postObj('/receiveAndDeliver/saveReceiveAndDeliverAddress', {
      userId: this.data.userId,
      receiveStatus: this.data.receiveStatus,
      name: this.data.name,
      phone: this.data.phone,
      province: this.data.province,
      city: this.data.city,
      region: this.data.region,
      detailAddress: this.data.detailAddress,
      regionalCode: this.data.areaCode
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '新增收货地址成功',
          icon: 'none',
          duration: 1000
        })
        wx.navigateBack({
          delta: 0,
        })
      }
    })
  },

  selectArea(area) {
    let data = area.detail.values
    this.setData({
      selectVisible: false,
      province: data[0].name,
      city: data[1].name,
      region: data[2].name,
      areaCode: data[2].code
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let addFlag = false
    let area = {
      name: '',
      phone: '',
      province: '广东省',
      city: '深圳市',
      region: '福田区',
      detailAddress: '',
      areaCode: '440304',
      receiveStatus: 1,
      id: ''
    }
    if (options.data) {
      addFlag = false
      area = JSON.parse(options.data)
    } else {
      addFlag = true
    }

    this.setData({
      id: area.id,
      name: area.name || '',
      phone: area.phone || '',
      province: area.province || '广东省',
      city: area.city || '深圳市',
      region: area.region || '福田区',
      detailAddress: area.detailAddress || '',
      areaCode: area.areaCode || '440304',
      addFlag: addFlag,
      userId: wx.getStorageSync('userId'),
      receiveStatus: area.receiveStatus
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})